<?php

namespace Xin\Coze\Providers;

use GuzzleHttp\Exception\GuzzleException;
use Psr\SimpleCache\InvalidArgumentException;
use Xin\Coze\SteamIterator;

class DatasetsProvider extends AbstractProvider
{

	/**
	 * 获取知识库列表
	 * @return array|null
	 */
	public function lists(array $query = [], array $options = [])
	{
		return $this->coze->httpGet('datasets', $query, $options);
	}

	/**
	 * 创建知识库
	 * @param string $name
	 * @param array{
	 *     space_id?:string,
	 *     format_type?:int,
	 *     description?:string,
	 *     file_id?:string,
	 * } $data
	 * @param array $options
	 * @return array|null
	 * @description $data.format_type: 0:文本类型 1:图片类型
	 * @description $data.file_id: 文件ID，知识库图标
	 */
	public function create(string $name, array $data = [], array $options = [])
	{
		return $this->coze->httpPost('/v1/datasets', array_merge($data, [
			'name' => $name,
		]), $options);
	}


	/**
	 * 更新知识库
	 * @param string $datasetId
	 * @param array{
	 *     name?:string,
	 *     description?:string,
	 *     file_id?:string,
	 * } $data
	 * @param array $options
	 * @return array|null
	 */
	public function update(string $datasetId, array $data = [], array $options = [])
	{
		return $this->coze->httpPut("/v1/datasets/{$datasetId}", $data, $options);
	}


	/**
	 * 删除知识库
	 * @param string $datasetId
	 * @param array $options
	 * @return array|null
	 */
	public function delete(string $datasetId, array $options = [])
	{
		return $this->coze->httpDelete("/v1/datasets/{$datasetId}", [], $options);
	}

	/**
	 * 创建知识库文档
	 * @param string $datasetId
	 * @param array{
	 *     name?:string,
	 *     source_info:array{
	 *         document_source?:int,
	 *         file_base64?:string,
	 *         file_type?:string,
	 *         source_file_id?:int,
	 *         web_url?:string,
	 *         update_rule?:array{
	 *             update_interval?:int,
	 *             update_type?:int,
	 *         },
	 *     },
	 *     content?:string,
	 *     file_id?:string,
	 * } $documentBases
	 * @param array{
	 *     chunk_strategy?:array{
	 *         chunk_type?:int,
	 *         max_tokens?:int,
	 *         remove_extra_spaces?:bool,
	 *         remove_urls_emails?:bool,
	 *         separator?:string,
	 *         chunk_size?:int,
	 *         format_type?:int,
	 *     },
	 * } $data
	 * @param array $options
	 * @return array|null
	 */
	public function documentCreate(string $datasetId, array $documentBases, array $data = [], array $options = [])
	{
		return $this->coze->httpPost(
			"/open_api/knowledge/document/create",
			array_merge(
				$data,
				[
					'dataset_id' => $datasetId,
					'document_bases' => $documentBases,
				],
			),
			$options
		);
	}

	/**
	 * 更新知识库文档
	 * @param string $documentId
	 * @param array{
	 *     document_name?:string,
	 *     update_rule?:array{
	 *         update_interval?:int,
	 *         update_type?:int,
	 *      }
	 *  } $data
	 * @param array $options
	 * @return array|null
	 */
	public function documentUpdate(string $documentId, array $data = [], array $options = [])
	{
		return $this->coze->httpPost(
			"/open_api/knowledge/document/update",
			array_merge(
				$data,
				[
					'document_id' => $documentId,
				]
			),
			$options
		);
	}

	/**
	 * 删除知识库文档
	 * @param array<string> $documentIds
	 * @return array|null
	 */
	public function documentsDelete(array $documentIds, array $options = [])
	{
		return $this->coze->httpPost(
			"/open_api/knowledge/document/delete",
			[
				'document_ids' => $documentIds,
			],
			$options
		);
	}

	/**
	 * 获取知识库文档列表
	 * @param string $datasetId
	 * @param int $page
	 * @param int $pageSize
	 * @param array $options
	 * @return array|null
	 */
	public function documentList(string $datasetId, int $page = 1, int $pageSize = 10, array $options = [])
	{
		return $this->coze->httpPost(
			"/open_api/knowledge/document/list",
			[
				'dataset_id' => $datasetId,
				'page' => $page,
				'page_size' => $pageSize,
			],
			$options
		);
	}

	/**
	 * 知识库文档上传进度
	 * @param string $datasetId
	 * @param array<string> $documentIds
	 * @param array $options
	 * @return array|null
	 */
	public function documentProcess(string $datasetId, array $documentIds, array $options = [])
	{
		return $this->coze->httpPost(
			"/v1/datasets/{$datasetId}/process",
			[
				'document_ids' => $documentIds,
			],
			$options
		);
	}

	/**
	 * 获取知识库图片列表
	 * @param string $datasetId
	 * @param int $page
	 * @param int $pageSize
	 * @param array{
	 *     keyword?:string,
	 *     has_caption?:bool,
	 * } $data
	 * @return array|null
	 * @throws GuzzleException
	 * @throws InvalidArgumentException
	 */
	public function imageList(string $datasetId, int $page = 1, int $pageSize = 10, array $data = [], array $options = [])
	{
		return $this->coze->httpPost(
			"/v1/datasets/{$datasetId}/images",
			array_merge([
				'page_num' => $page,
				'page_size' => $pageSize,
			], $data),
			$options
		);
	}
}
